function main(){
    start();
    let student_array=[];
    for(let option=input_option();;){
        if(option==1){
            let one=optionone();
            student_array.push(one);
            console.log(`学生${one.name}的成绩被添加`);
            start();
            option=input_option();
        }
        else if(option==2){
            let list=optionTwo(student_array);
            console.log(list);
            start();
            option=input_option();
        }
        else if(option==3){ 
            break;
        }
        else{
            console.log("请重新输入");
            option=input_option();
        }
    }   
}

function start(){
    console.log(`1. 添加学生
2. 生成成绩单
3. 退出
请输入你的选择（1～3）：`);
}

function input_option(){
    let readlineSync=require('readline-sync');
    let option=readlineSync.question('');
    return option;
}

function optionone(){
    let readlineSync=require('readline-sync');
    console.log('请输入学生信息（格式：姓名, 学号, 民族, 班级, 学科: 成绩, ...）');
    let student=readlineSync.question('');
    let pattern=/^(\w+\,?)|(\w+\:\d+\,?)+|(\d+)$/g;
    let array=[];
    let student_object=new Object();
    if(!pattern.test(student)){
        console.log('请按正确的格式输入（格式：姓名, 学号, 学科: 成绩, ...）');
        student=readlineSync.question('');
    }
    else{
        array=student.split(",");
        student_object=s(array);
    }
    return student_object;
}

function s(array){
    let a=new Object();
    a.name=array[0];
    a.id=array[1];
    a.nation=array[2];
    a.class=array[3];
    for(let i=4;i<array.length;i++){
        let l=array[i].indexOf(":")
        let subject=array[i].substring(0,l);
        let score=array[i].replace(/[^0-9]/ig,"");
        a[subject]=score;
    }
    a.total=parseInt(a.math)+parseInt(a.chinese)+parseInt(a.english)+parseInt(a.programming);
    a.average=a.total/4;
    return a;
}

function optionTwo(student_array){
    let readlineSync=require('readline-sync');
    console.log('请输入要打印的学生的学号（格式： 学号, 学号,...）');
    let id=readlineSync.question('');
    let pattern=/^(\d+\,?)+|(\d+)$/;
    let array=[];
    let list;
    if(!pattern.test(id)){
        console.log('请按正确的格式输入要打印的学生的学号（格式： 学号, 学号,...）');
        id=readlineSync.question('');
    }
    else{
        array=id.split(",");
        list=get_list(array,student_array);
        console.log(array);
    }
    return list;
}

function get_list(id,student_array){
    let list=`成绩单
    姓名|数学|语文|英语|编程|平均分|总分 
    ========================
`;
    let sum=0;
    let n=0;
    let class_total=[];
    for(let i=0;i<id.length;i++){
        for(let j=0;j<student_array.length;j++){
            if(id[i]==student_array[j].id){
                sum+=student_array[j].total;
                n+=1;
                class_total.push(student_array[j].total);
                list+=`${student_array[j].name}|${student_array[j].math}|${student_array[j].chinese}|${student_array[j].english}|${student_array[j].programming}|${student_array[j].average}|${student_array[j].total}
`;
            }
        }
    }
    let class_average=sum/n;
    class_total.sort();
    let class_median;
    if(class_total.length%2==0){
        class_median=(class_total[class_total.length/2]+class_total[class_total.length/2-1])/2;
    }
    else{
        class_median=class_total[(class_total.length-1)/2];
    }
    list+=`========================
    全班总分平均数：${class_average}
    全班总分中位数：${class_median}`;
    return list;

}

exports.main=main;
exports.start=start;
exports.optionone=optionone;
exports.optionTwo=optionTwo;
exports.s=s;
exports.get_list=get_list;